/**
 * Title : jFamilyTree browser
 * Description: jFamilyTree browser
 * About: Designed and rewritten by Andreas Lundgren, based on the Treebolic application suite
 * Copyright:	(c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.browser;

import java.awt.Image;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import jFamilyTree.IContext;
import jFamilyTree.IWidget;

/**
 * Context
 * 
 * @author Bernard Bou
 */
/**
 * @author bbou
 */
/**
 * @author bbou
 */
public class Context implements IContext
{
	/**
	 * Browser
	 */
	private final MainFrame theBrowser;

	/**
	 * Connected Widget
	 */
	private IWidget theWidget;

	/**
	 * Source
	 */
	private String theSource;

	/**
	 * Source
	 */
	private String theBase;

	/**
	 * Source
	 */
	private String theImageBase;

	/**
	 * Constructor
	 */
	public Context(MainFrame thisApplication, String thisSource, String thisBase, String thisImageBase)
	{
		theBrowser = thisApplication;
		theWidget = null;
		theSource = thisSource;
		theBase = thisBase;
		theImageBase = thisImageBase;
	}

	/**
	 * Connected Widget
	 */
	public void connect(IWidget thisWidget)
	{
		theWidget = thisWidget;
	}

	/*
	 * (non-Javadoc)
	 * @see jFamilyTree.IContext#getBase()
	 */
	public URL getBase()
	{
		URL thisDefaultBase = null;
		if (theBase != null && !theBase.isEmpty())
		{
			// 1-use 'base' parameter as full-fledged
			try
			{
				return new URL(theBase);
			}
			catch (MalformedURLException e)
			{
			}

			// 1b-use 'base' parameter as full-fledged directory
			try
			{
				File thisFile = new File(theBase);
				if (thisFile.exists() && thisFile.isDirectory())
					return thisFile.toURI().toURL();
			}
			catch (MalformedURLException e)
			{
			}

			// 2-use 'base' parameter as path relative to default
			String thisBase = theBase;
			if (!thisBase.endsWith("/")) //$NON-NLS-1$
				thisBase += "/"; //$NON-NLS-1$
			thisDefaultBase = getDefaultBase();
			try
			{
				return new URL(thisDefaultBase, thisBase);
			}
			catch (MalformedURLException e)
			{
			}
		}

		// 3-default
		if (thisDefaultBase == null)
			thisDefaultBase = getDefaultBase();
		return thisDefaultBase;
	}

	/*
	 * (non-Javadoc)
	 * @see jFamilyTree.IContext#getImagesBase()
	 */
	public URL getImagesBase()
	{
		URL thisDocumentUrl = null;
		URL thisDefaultUrl = null;
		if (theImageBase != null && !theImageBase.isEmpty())
		{
			// 1-use 'images' parameter as full-fledged url
			try
			{
				return new URL(theImageBase);
			}
			catch (Exception thisException)
			{
			}

			// 1b-use 'base' parameter as full-fledged directory
			try
			{
				File thisFile = new File(theImageBase);
				if (thisFile.exists() && thisFile.isDirectory())
					return thisFile.toURI().toURL();
			}
			catch (MalformedURLException e)
			{
			}

			String thisImageBase = theImageBase;
			if (!thisImageBase.endsWith("/")) //$NON-NLS-1$
				thisImageBase += "/"; //$NON-NLS-1$

			// 2- use 'images' parameter as relative to document base
			try
			{
				thisDocumentUrl = makeURL(theSource);
				return new URL(thisDocumentUrl, thisImageBase);
			}
			catch (Exception thisException)
			{
			}

			// 3- use 'images' parameter as relative to default
			try
			{
				thisDefaultUrl = getDefaultImagesBase();
				return new URL(thisDefaultUrl, thisImageBase);
			}
			catch (Exception thisException)
			{
			}
		}

		// 4- document base + "images/"
		try
		{
			if (thisDocumentUrl == null)
				thisDocumentUrl = makeURL(theSource);
			return new URL(thisDocumentUrl, "images/"); //$NON-NLS-1$
		}
		catch (Exception thisException)
		{
		}

		// 5- default + "images/"
		try
		{
			if (thisDefaultUrl == null)
				thisDefaultUrl = getDefaultImagesBase();
			return new URL(thisDefaultUrl, "images/"); //$NON-NLS-1$
		}
		catch (Exception thisException)
		{
		}

		// default
		return thisDefaultUrl;
	}

	/*
	 * (non-Javadoc)
	 * @see jFamilyTree.Context#getHtmlDocumentBase()
	 */
	public URL getHtmlDocumentBase()
	{
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see jFamilyTree.component.Context#getImage(java.net.URL)
	 */
	public Image getImage(URL thisURL)
	{
		return java.awt.Toolkit.getDefaultToolkit().getImage(thisURL);
	}

	/*
	 * (non-Javadoc)
	 * @see jFamilyTree.component.Context#getParameter(java.lang.String)
	 */
	public String getParameter(String thisName)
	{
		return theBrowser.getParameter(thisName);
	}

	/*
	 * (non-Javadoc)
	 * @see jFamilyTree.component.Context#linkTo(java.lang.String)
	 */
	public boolean linkTo(String thisLinkUrl, String thisLinkTarget)
	{
		theBrowser.linkTo(thisLinkUrl, thisLinkTarget, theWidget, this);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * @see jFamilyTree.component.Context#showStatus(java.lang.String)
	 */
	public void showStatus(String thisString)
	{
		System.out.println(Messages.getString("Context.0") + thisString); //$NON-NLS-1$
	}

	// U R L F A C T O R Y

	/**
	 * Make url
	 * 
	 * @param thisSource
	 *        source
	 * @return url
	 * @throws MalformedURLException
	 */
	protected URL makeURLAlt(String thisSource) throws MalformedURLException
	{
		// try to consider it well-formed full-fledged url
		try
		{
			return new URL(thisSource);
		}
		catch (MalformedURLException e)
		{
		}

		// try to consider it file
		File thisFile = new File(thisSource);
		if (thisFile.exists() && thisFile.canRead())
			try
			{
				return thisFile.toURI().toURL();
			}
			catch (MalformedURLException thisException)
			{
			}

		// default to source relative to a base
		return new URL(getDefaultBase(), thisSource);
	}

	/**
	 * Make url
	 * 
	 * @param thisSource
	 *        source
	 * @return url
	 */
	protected URL makeURL(String thisSource)
	{
		if (thisSource == null)
			return null;

		// try to consider it well-formed full-fledged url
		try
		{
			return new URL(thisSource);
		}
		catch (MalformedURLException e)
		{
		}

		// default to source relative to a base
		try
		{
			return new URL(getBase(), thisSource);
		}
		catch (MalformedURLException e)
		{
		}
		return null;
	}

	/**
	 * Get base default
	 * 
	 * @return base default;
	 */
	protected URL getDefaultBase()
	{
		// base parameter
		String thisBase = getParameter("base"); //$NON-NLS-1$
		String thisURLString = thisBase != null ? thisBase : System.getProperty("user.dir"); //$NON-NLS-1$

		// tail
		if (!thisURLString.endsWith("/")) //$NON-NLS-1$
			thisURLString += "/"; //$NON-NLS-1$

		// make
		try
		{
			return new URL(thisURLString);
		}
		catch (MalformedURLException e)
		{
			// make from folder
			try
			{
				File thisFolder = new File(thisURLString);
				return thisFolder.toURI().toURL();
			}
			catch (MalformedURLException thisException)
			{
			}
		}
		return null;
	}

	/**
	 * Get images base default
	 * 
	 * @return image base default;
	 */
	protected URL getDefaultImagesBase()
	{
		URL thisBase = getDefaultBase();
		try
		{
			return new URL(thisBase, "images/"); //$NON-NLS-1$
		}
		catch (MalformedURLException thisException)
		{
		}
		return null;
	}
}
